package com.duanyf.wiki.controller;


import com.duanyf.wiki.req.CategoryQueryReq;
import com.duanyf.wiki.req.CategorySaveReq;
import com.duanyf.wiki.resp.CategoryQueryResp;
import com.duanyf.wiki.resp.CommonResp;
import com.duanyf.wiki.resp.PageResp;
import com.duanyf.wiki.service.CategoryService;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: duanyf
 * @DateTime: 2022/9/15 0015 22:46
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

  @Resource
  private CategoryService categoryService;

  @GetMapping("/all")
  public CommonResp all(@Valid CategoryQueryReq req){
    //此处泛型就是实际返回业务数据的类型，即CommonResp中content的类型
    CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
    List<CategoryQueryResp> list = categoryService.all(req);
    resp.setContent(list);
    return resp;
  }

  @GetMapping("/list")
  public CommonResp list(@Valid CategoryQueryReq req){
    //此处泛型就是实际返回业务数据的类型，即CommonResp中content的类型
    CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
    PageResp<CategoryQueryResp> list = categoryService.list(req);
    resp.setContent(list);
    return resp;
  }

  @PostMapping("/save")
  public CommonResp save(@Valid @RequestBody CategorySaveReq req){
    //此处泛型就是实际返回业务数据的类型，即CommonResp中content的类型
    CommonResp resp = new CommonResp();
    categoryService.save(req);
    return resp;
  }

  @DeleteMapping("/delete/{id}")
  public CommonResp delete(@PathVariable Long id){
    //此处泛型就是实际返回业务数据的类型，即CommonResp中content的类型
    CommonResp resp = new CommonResp();
    categoryService.delete(id);
    return resp;
  }
}
